
package com.be.beadmin.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.exception.BeadminException;
import com.be.beadmin.common.utils.JpaQueryBuilder;
import com.be.beadmin.common.utils.JpaSortBuilder;
import com.be.beadmin.service.dto.input.DictTypeDto;
import com.be.beadmin.service.dto.output.DictTypeEx;
import com.be.beadmin.service.entity.DictType;
import com.be.beadmin.service.entity.DictType_;
import com.be.beadmin.service.mapper.DictTypeMapper;
import com.be.beadmin.service.mapper.DictTypeMapperEx;
import com.be.beadmin.service.repository.DictTypeRepository;
import com.be.beadmin.service.service.DictTypeService;
import com.be.beadmin.service.service.SerialNumberService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Order;
import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author zjm
 * @description 服务实现
 * @date 2023-02-02
 **/
@Service
@RequiredArgsConstructor
public class DictTypeServiceImpl implements DictTypeService {

    @Autowired
    private DictTypeRepository dictTypeRepository;
    @Autowired
    private DictTypeMapper dictTypeMapper;
    @Autowired
    private DictTypeMapperEx dictTypeExMapper;
    @Autowired
    private SerialNumberService serialNumberService;

    @Override
    public Page<DictTypeEx> getDictTypeByPage(PageEntity pageEntity) {
        Pageable pageable = PageRequest.of(
                pageEntity.getCurrent() - 1,
                pageEntity.getPageSize());
        Page<DictType> page = dictTypeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {
                    List<Predicate> list = new ArrayList<>();
                    Predicate pre1 = JpaQueryBuilder.getPredicate(root, pageEntity.getFilters(), criteriaBuilder);
                    list.add(pre1);
                    Predicate pre2 = criteriaBuilder.equal(root.get(DictType_.delete), 0); //过滤为0
                    list.add(pre2);
                    Predicate[] p = new Predicate[list.size()];
                    List<Order> orders = new ArrayList<>();
                    if (pageEntity.getSorters() != null && pageEntity.getSorters().size() > 0) {
                        orders = JpaSortBuilder.getPredicate(root, pageEntity.getSorters(), criteriaBuilder);
                        criteriaQuery.orderBy(orders);
                    } else {
                        criteriaQuery.orderBy(criteriaBuilder.desc(root.get(DictType_.createTime).as(Date.class)));
                    }
                    return criteriaBuilder.and(list.toArray(p));
                }
                , pageable);
        Page<DictTypeEx> page1 = page.map(dictTypeExMapper::toEntity);
        return page1;

    }

    @Override
    public void addDictType(DictTypeDto dictTypeDto) {
        DictType result = dictTypeRepository.findDictTypeByIdAndDelete(dictTypeDto.getId(), 0);
        if (ObjectUtil.isNotEmpty(result)) {
            throw new BeadminException("DictType不存在,请重新查询");
        }
        DictType resultName = dictTypeRepository.findDictTypeByDictNameAndDelete(dictTypeDto.getDictName(), 0);
        if (resultName != null) {
            throw new BeadminException("已经存在字典名为："+dictTypeDto.getDictName()+"的字典类型，请勿重复添加！！");
        }

        // 设置字典类型编码
        String code = serialNumberService.getNextSnByCode("DictTypeCode");
        dictTypeDto.setDictCode(code);

        DictType dictType = dictTypeMapper.toEntity(dictTypeDto);
        dictTypeRepository.save(dictType);
    }

    @Override
    public void updateDictType(DictTypeDto dictTypeDto) {
        DictType dictType = dictTypeRepository.findDictTypeById(dictTypeDto.getId());
        DictType dictType2 = dictTypeMapper.toEntity(dictTypeDto);
        dictType2.setDictCode(dictType.getDictCode());
        dictTypeRepository.save(dictType2);
    }


    @Override
    public void delDictType(String id) {
        DictType dictType = dictTypeRepository.findDictTypeById(id);
        dictType.setDelete(1);
        dictTypeRepository.save(dictType);
    }

    @Override
    public void delDictTypes(List<String> ids) {
    }

    @Override
    public DictType getDictTypeByName(String name) {
        DictType dictType = dictTypeRepository.findDictTypeByDictName(name);
        System.out.println("dictType:" + dictType);
//        DictTypeEx dictTypeEx =  dictTypeExMapper.toEntity(dictType);
//        System.out.println("dictType:"+dictType);
        return dictType;
    }
}